package com.jpress.beans;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.primefaces.context.RequestContext;

import com.jpress.controller.ComentarioController;
import com.jpress.controller.CustomPageController;
import com.jpress.controller.PostController;
import com.jpress.exceptions.CrudException;
import com.jpress.model.Blog;
import com.jpress.model.Comentario;
import com.jpress.model.CustomPage;
import com.jpress.model.Post;
import com.jpress.utils.FacesUtils;

@ManagedBean(name="BlogBean")
@SessionScoped
public class BlogBean implements Serializable{
	
	private static final long serialVersionUID = -1317879585523606230L;
	
	@ManagedProperty(value="#{SessaoBean}")
	private SessaoBean sessaoBean;
	
	private Blog blog;
	
	private List<Post> posts;
	private List<Post> postsPesquisa;
	private Post post;
	
	private List<CustomPage> customPages;
	private CustomPage customPage;
	
	private List<Comentario> comentarios;
	private Comentario comentario;
	
	private PostController postController;
	private CustomPageController customPageController;
	private ComentarioController comentarioController;
	
	private String tituloPostPesquisar;
	private String conteudoPostPesquisar;
	
	@PostConstruct
	private void init(){
		comentario = new Comentario();
		carregarInstanciasControladores();
	}

	public SessaoBean getSessaoBean() {
		return sessaoBean;
	}

	public void setSessaoBean(SessaoBean sessaoBean) {
		this.sessaoBean = sessaoBean;
	}

	public Blog getBlog() {
		return blog;
	}

	public void setBlog(Blog blog) {
		this.blog = blog;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}

	public List<Post> getPostsPesquisa() {
		return postsPesquisa;
	}

	public void setPostsPesquisa(List<Post> postsPesquisa) {
		this.postsPesquisa = postsPesquisa;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}

	public List<CustomPage> getCustomPages() {
		return customPages;
	}

	public void setCustomPages(List<CustomPage> customPages) {
		this.customPages = customPages;
	}

	public CustomPage getCustomPage() {
		return customPage;
	}

	public void setCustomPage(CustomPage customPage) {
		this.customPage = customPage;
	}

	public List<Comentario> getComentarios() {
		return comentarios;
	}

	public void setComentarios(List<Comentario> comentarios) {
		this.comentarios = comentarios;
	}

	public Comentario getComentario() {
		return comentario;
	}

	public void setComentario(Comentario comentario) {
		this.comentario = comentario;
	}

	public String getTituloPostPesquisar() {
		return tituloPostPesquisar;
	}

	public void setTituloPostPesquisar(String tituloPostPesquisar) {
		this.tituloPostPesquisar = tituloPostPesquisar;
	}

	public String getConteudoPostPesquisar() {
		return conteudoPostPesquisar;
	}

	public void setConteudoPostPesquisar(String conteudoPostPesquisar) {
		this.conteudoPostPesquisar = conteudoPostPesquisar;
	}

	private void carregarInstanciasControladores() {
		postController = PostController.getInstance();
		comentarioController = ComentarioController.getInstance();
		customPageController = CustomPageController.getInstance();
	}	

	public String exibirBlog(Blog blog){
		this.setBlog(blog);
		setPosts(postController.findAllByBlogCodigo(blog.getCodigo()));
		setCustomPages(customPageController.findAllByBlogCodigo(blog.getCodigo()));
		return "/paginas/blogs/blog.jsf" + "?faces-redirect=true";
	}
	
	public String blogInicio(){
		return "/paginas/blogs/blog.jsf" + "?faces-redirect=true";
	}
	
	public String exibirPost(Post post){
		this.setPost(post);
		setComentarios(comentarioController.findAllByPostCodigo(post.getCodigo()));
		return "/paginas/blogs/post.jsf" + "?faces-redirect=true";
	}
	
	public String exibirCustomPage(CustomPage customPage){
		this.setCustomPage(customPage);
		return "/paginas/blogs/custompage.jsf" + "?faces-redirect=true";
	}
	
	public String pesquisarPost(){
		this.postsPesquisa = postController.pesquisarByBlogCodigo(tituloPostPesquisar, conteudoPostPesquisar, blog.getCodigo()); 
		return "/paginas/blogs/pesquisa.jsf"  + "?faces-redirect=true";
	}
	
	public void comentar(){
		comentario.setAutor(sessaoBean.getUsuarioAutenticado());
		comentario.setPost(post);
		comentario.setData(Calendar.getInstance().getTime());
		try {
			comentarioController.inserir(comentario);
			FacesUtils.addSucessMessage("blogGrowl", FacesUtils.getParamMessageBundle("crud.sucessoMensagens.insercao", "Comentario"));
			RequestContext.getCurrentInstance().update("blogGrowl");
			setComentarios(comentarioController.findAllByPostCodigo(post.getCodigo()));
			comentario = new Comentario();
		} catch (CrudException e) {
			FacesUtils.addErrorMessage("cadastroMessages", e.getMessage());
		}
	}
	
	
	
}
